home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-06-05 | 2.0 KB | 98 lines | [MATS/MATL] |
- echo off;
- % NUMERICAL METHODS: MATLAB Programs, (c) John H. Mathews 1994
- % To accompany the text:
- % NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed, 1992
- % Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
- % This free software is complements of the author.
-
- % Algorithm 7.1 (Composite Trapezoidal Rule).
- % Section 7.2, Composite Trapezoidal and Simpson's Rule, Page 365
- echo on; clc; format long; hold off; clear
-
- % This program implements the trapezoidal rule.
-
- % The function f(x) is integrated over the interval [a,b].
-
- % Define and store the function f(x) in the M-file f.m
- % function y = f(x)
- % y = 1 + exp(-x).*sin(4*x);
-
- delete f.m
- diary f.m; disp('function y = f(x)');...
- disp('y = 1 + exp(-x).*sin(4*x);');...
- diary off;
-
- f(0); % Remark. f.m traprl.m grpoly.m are used for Algorithm 7.1
-
- pause % Press any key to continue.
-
- clc;, clg;
- % Plot f(x) over the interval [a,b].
-
- a = 0;
- b = 1;
- h = (b-a)/200;
- X = a:h:b;
- Y = f(X);
-
- pause % Press any key to see the graph y = f(x).
-
- clc; clg;
- c = 0;
- d = 2;
- axis([a b c d]);...
- plot(X,Y,'g');...
- hold on;...
- plot([a b],[0 0],'b',[0 0],[c d],'b');...
- xlabel('x');...
- ylabel('y');...
- title('The curve y = f(x) over [a,b].');...
- grid;...
- axis;...
- hold off;...
- shg; pause % Press any key to continue.
-
- clc;
-
- % The trapezoidal rule is applied over the interval [a,b].
-
- % Place the endpoints of [a,b] in a and b.
-
- % Place the number of subintervals in m.
-
- a = 0;
- b = 1;
- m = 8;
-
- s = traprl('f',a,b,m);
-
- pause % Press any key to continue.
-
- clc;
- Mx1 = 'The trapezoidal rule applied with m = ';
- Mx2 = [Mx1,num2str(m),' subintervals is:'];
- clc,disp(''),disp(Mx2),disp(''),disp(s)
-
- pause % Press any key to see a nice graph y = f(x).
-
- clc; clg;
- c = 0;
- d = 2;
- axis([a b c d]);...
- plot(X,Y,'g');...
- hold on;...
- plot([a b],[0 0],'b',[0 0],[c d],'b');...
- xlabel('x');...
- ylabel('y');...
- title('The trapezoidal rule.');...
- if m <= 50,
- grpoly(a,b,m,1);
- end;...
- grid;...
- axis;...
- hold off;...
- shg; pause % Press any key to continue.
-
- clc,echo off,diary output,...
- disp(''),disp(Mx2),disp(''),disp(s),diary off,echo on
-